package com.example.myapplicationtest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
  String path="https://www.wanandroid.com/user/login";

    public   Button button_d;
    public  Button button_z;
    String username;
    String password;
    EditText et_username, et_password;
    Intent intent;
    AlertDialog dialog;


    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg){
            if(msg.what==1){
                int  a =(Integer) msg.obj;
                startActivity(intent);

            }else{
                String ss=(String)msg.obj;
                dialog.setTitle("消息提示框");
                dialog.setIcon(R.mipmap.qq);
                dialog.setMessage(ss);
                dialog.show();

            }
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button_d =(Button) findViewById(R.id.button1);
        Button button_a =(Button) findViewById(R.id.button2);
        et_username  =(EditText)findViewById(R.id.et_nummber);
        et_password  =(EditText)findViewById(R.id.et_nummber1);

       intent = new Intent(this,Appmain.class);
        dialog=new AlertDialog.Builder(this).create();
    }
    public void click1(View v){
        username = et_username.getText().toString().trim();
        password = et_password.getText().toString().trim();
     new Thread(){
         private HttpURLConnection coon;

         @Override
         public void run() {

             try {
                 URL url=new URL(path);
            coon=(HttpURLConnection)url.openConnection();
            coon.setRequestMethod("POST");
            coon.setReadTimeout(5000);
            coon.setConnectTimeout(5000);
            coon.setDoInput(true);
           coon.setDoOutput(true);
           coon.setUseCaches(false);
           String data="username="+URLEncoder.encode(username,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8");
                 OutputStream out =coon.getOutputStream();
                 out.write(data.getBytes());
                 out.flush();
                 if(coon.getResponseCode()==200){
                     InputStream is=coon.getInputStream();
                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                         byte[] buffer = new byte[1024];
                         int len = 0;
                         while((len = is.read(buffer)) != -1)
                         {
                             outStream.write(buffer,0,len);
                         }
                         is.close();
                    byte[] data1 =outStream.toByteArray();;
                    String html = new String(data1, "UTF-8");
                     System.out.println(html);
                    Gson gson=new Gson();
                    LoginBean loginBean = gson.fromJson(html, LoginBean.class);
                     int  errorMsg =loginBean.getErrorCode();
                     if(errorMsg==0){
                         Message msg = new Message();
                         msg.what=1;
                         msg.obj=errorMsg;
                         handler.sendMessage(msg);
                     }else{
                         Message msg1 = new Message();
                         msg1.what=0;
                         msg1.obj=loginBean.getErrorMsg();
                         handler.sendMessage(msg1);


                     }
                 }

                 else{
                     Toast.makeText(MainActivity.this,"服务器相应失败",Toast.LENGTH_SHORT).show();
                 }

             } catch (Exception e) {
                 e.printStackTrace();
             }

         }
     }.start();


    }
    public void click2(View v)
    {
        Intent intent1 = new Intent(this,regist.class);
        startActivity(intent1);


    }




}